﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bymed.SpineScan.DataModel;

namespace Bymed.SpineScan.Service.Validation
{
    public class CustomizedTestItemValidator : DataValidatorBase<CustomizedTestItem>
    {
        public const string ItemTitleErrorKey = "ItemTitle";
        public const string ItemTypeErrorKey = "ItemType";

        /// <summary>
        /// Validate the provided entity. Then check the IsValid property to see the result.
        /// </summary>
        /// <param name="entity">The provided entity to be validated.</param>
        public override void Validate(CustomizedTestItem entity)
        {
            CheckRequiredItemTitle(entity);
            CheckItemTitleLength(entity);
        }

        /// <summary>
        /// Check if the required item title is provided.
        /// </summary>
        /// <param name="entity">The instance of CustomizedTestItem.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredItemTitle(CustomizedTestItem entity)
        {
            if (!ValidationHelper.RuleCheckRequiredContent(entity.ItemTitle))
            {
                AddError(ItemTitleErrorKey, "项目名称不能为空");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the item title's max length.
        /// </summary>
        /// <param name="entity">The instance of CustomizedTestItem.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckItemTitleLength(CustomizedTestItem entity)
        {
            int maxLength = 10;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.ItemTitle))
            {
                AddError(ItemTitleErrorKey, string.Format("项目名称长度不能超过{0}", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check if the required item type is provided.
        /// </summary>
        /// <param name="entity">The instance of CustomizedTestItem.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredItemType(CustomizedTestItem entity)
        {
            if (!ValidationHelper.RuleCheckRequiredContent(entity.ItemType))
            {
                AddError(ItemTypeErrorKey, "项目类型不能为空");
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
